<?php

namespace App\Http\Controllers\Api;

use App\Api\Sms\SmsClient;
use App\Models\Sms\SmsSendLogModel;
use App\Models\Sms\SmstmpModel;
use App\Repository\Sms\SmsReplyRepository;
use Illuminate\Http\Request;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class SmsPushController extends BaseController
{
    public function __construct()
    {
        $this->log = new Logger('SmsPushData');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/SmsPushData.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

//旦米短信发送是否成功的推送结果
    public function sendPushResult(Request $Request)
    {
        $data = $Request->all();
        $this->log->info('SmsPushData:' . json_encode($Request->all()));
        // $getData = 'name=401325&mo=%5B%7B%22reference%22%3A%22%22%2C%22msg_fmt%22%3A8%2C%22src%22%3A%2215378736962%22%2C%22receive_time%22%3A%222022-07-07+ 11%3A02%3A49%22%2C%22sign%22%3A%22%25E5%2585%25A5%25E7%25BD%2591%25E9%2580%259A%25E7%259F%25A5%22%2C%22dest%22%3A%22%22%2C%22content%22%3A%22%2B%25E5%2593%2588%25E5%2596%25BD%22%2C%22binary_content%22%3A%22%22%7D%5D';
        if (empty($data) && !isset($data['smsResult'])) {
            return 'error';
        }
        if (empty($data['smsResult'])) {
            return 'error';
        }
        $ModelLogs = new SmsSendLogModel();
        foreach ($data['smsResult'] as $item) {
            if (!isset($item['smsId']) || empty($item['smsId'])) {
                continue;
            }
            $upData = [
                'status' => $item['status'],
                'charging_num' => $item['chargingNum']
            ];
            if ($item['status'] == 0) {
                $upData['is_send'] = 2;
            }
            if ($item['status'] == 1) {
                $upData['remark'] = $item['respMessage'];
            }
            $ModelLogs->where(['sms_id' => $item['smsId'], 'phone' => $item['phone']])->update($upData);
        }
        return 'success';
    }

//Shlianlu短信发送是否成功的推送结果
    public function sendPushResultShlianlu(Request $Request)
    {
        $item = $Request->all();
        $this->log->info('SmsPushData:' . json_encode($item));
        // $getData = 'name=401325&mo=%5B%7B%22reference%22%3A%22%22%2C%22msg_fmt%22%3A8%2C%22src%22%3A%2215378736962%22%2C%22receive_time%22%3A%222022-07-07+ 11%3A02%3A49%22%2C%22sign%22%3A%22%25E5%2585%25A5%25E7%25BD%2591%25E9%2580%259A%25E7%259F%25A5%22%2C%22dest%22%3A%22%22%2C%22content%22%3A%22%2B%25E5%2593%2588%25E5%2596%25BD%22%2C%22binary_content%22%3A%22%22%7D%5D';
        if (empty($item) && !isset($item['taskId'])) {
            return 'error';
        }
        $ModelLogs = new SmsSendLogModel();
        $smsId = $item['taskId'];
        $status = 0;
        if (intval($item['status']) !== 0) {
            $status = 1;
        }
        $upData = [
            'status' => $status,
            'charging_num' => 1
        ];
        if ($status === 0) {
            $upData['is_send'] = 2;
        }
        if ($item['status'] == 1) {
            $upData['remark'] = $item['codeDesc'];
        }
        $ModelLogs->where(['sms_id' => $smsId, 'phone' => $item['phone']])->update($upData);

        return 'success';
    }

//    Shlianlu模板审核状态的推送结果
    public function tplPushResultShlianlu(Request $Request)
    {
        $item = $Request->all();
        $this->log->info('tplPushData:' . json_encode($item));
        // $getData = 'name=401325&mo=%5B%7B%22reference%22%3A%22%22%2C%22msg_fmt%22%3A8%2C%22src%22%3A%2215378736962%22%2C%22receive_time%22%3A%222022-07-07+ 11%3A02%3A49%22%2C%22sign%22%3A%22%25E5%2585%25A5%25E7%25BD%2591%25E9%2580%259A%25E7%259F%25A5%22%2C%22dest%22%3A%22%22%2C%22content%22%3A%22%2B%25E5%2593%2588%25E5%2596%25BD%22%2C%22binary_content%22%3A%22%22%7D%5D';
        if (empty($item) && !isset($item['id'])) {
            return 'error';
        }
        $ModelTpl = new SmstmpModel();
        $status = 0;
//        审核状态，1为通过，3为驳回
        if (intval($item['status']) === 1) {
            $status = 2;
        } else if (intval($item['status']) === 3) {
            $status = 3;
        }
        $upData = [
            'verify_status' => $status
        ];
        $ModelTpl->where(['tpl_id' => $item['id']])->update($upData);

        return 'success';
    }

//    旦米短信回复接收
    public function replyPushResult(Request $request)
    {
        $data = $request->all();
//        $this->log->info('SmsPushData:' . json_encode($Request->all()));
        // $getData = 'name=401325&mo=%5B%7B%22reference%22%3A%22%22%2C%22msg_fmt%22%3A8%2C%22src%22%3A%2215378736962%22%2C%22receive_time%22%3A%222022-07-07+ 11%3A02%3A49%22%2C%22sign%22%3A%22%25E5%2585%25A5%25E7%25BD%2591%25E9%2580%259A%25E7%259F%25A5%22%2C%22dest%22%3A%22%22%2C%22content%22%3A%22%2B%25E5%2593%2588%25E5%2596%25BD%22%2C%22binary_content%22%3A%22%22%7D%5D';
        $SmsApi = new SmsClient();
        if ($data['sig'] != $SmsApi->getSign($data)) {
            return 'error';
        }
        $Repository = new SmsReplyRepository();
        $Repository->unifiedAdd($request);
        return 'success';
    }

    // feige推送报告
    public function receivePushSendResult(Request $Request)
    {
        $postData = $Request->all();
        $this->log->info('FeigeSmsPushData:' . json_encode($postData));
        if (empty($postData) && !isset($postData['data'])) {
            return 'error';
        }
        $ModelLogs = new SmsSendLogModel();
        if (!is_array($postData['data'])) {
            return 'error';
        }
        foreach ($postData['data'] as $k => $item) {
            $smsId = $item['msg_no'];
            // 0成功
            $status = 0;
            if (strtoupper(trim($item['status'])) !== 'DELIVRD') {
                $status = 1;
            }
            $upData = [
                'status' => $status,
                'charging_num' => 1
            ];
            if ($status === 0) {
                $upData['is_send'] = 2;
            }
            if ($item['status'] == 1) {
                $upData['remark'] = $item['status_desc'];
            }
            $ModelLogs->where(['sms_id' => $smsId])->update($upData);
        }
        return 'success';
    }
}
